﻿#include "BRTree.h"

namespace Ysz
{

	template<class K >
	class set
	{
		struct KeyofT
		{

			const K& operator()(const K& x)
			{
				return x;
			}
		};
	public:
		typedef typename RBTree<K, const K, KeyofT>::iterator iterator;
		typedef typename RBTree<K, const K, KeyofT>::const_iterator const_iterator;


		iterator begin()
		{
			return bt_tree.begin();
		}
		iterator end()
		{
			return  bt_tree.end();
		}
		const_iterator begin() const
		{
			return bt_tree.begin();
		}
		const_iterator end() const
		{
			return  bt_tree.end();
		}
		set()
		{}

		set(const set<K>& s )
		{
			for (const auto& x : s)
			{
				insert(x);
			}
		}

		set(initializer_list<K> il)
		{
			for (const auto& x: il)
			{
				insert(x);
			}
		}
		
		template<class Iterator>
		set(Iterator first, Iterator end)
		{
			while (first != end)
			{
				insert(*first);
				first++;
			}
		}

		pair<iterator, bool> insert(const K& data)
		{
			return bt_tree.Insert(data);
		}
		
		iterator find(const K& data)
		{
			return bt_tree.find(data);
		}
		

	private:
		RBTree<K, const K, KeyofT> bt_tree;
	};





}

namespace Ysz
{



	template<class K ,class V>
	class map
	{
		struct KeyofT
		{

			const K & operator()(const pair<K,V> & kv)
			{
				return kv.first;
			}
		};

	public:

		typedef typename RBTree<K, pair<const K,V>, KeyofT>::iterator iterator;
		typedef typename RBTree<K, pair<const K,V>, KeyofT>::const_iterator const_iterator;


		iterator begin()
		{
			return bt_tree.begin();
		}
		iterator end()
		{
			return  bt_tree.end();
		}
		const_iterator begin() const
		{
			return bt_tree.begin();
		}
		const_iterator end() const
		{
			return  bt_tree.end();
		}
		map()
		{}

		map(const map<K,V>& s)
		{
			for (const auto& x : s)
			{
				insert(x);
			}
		}

		map(initializer_list<const pair< K, V>> il)
		{
			for (const auto& x : il)
			{
				insert(x);
			}
		}

		template<class Iterator>
		map(Iterator first, Iterator end)
		{
			while (first != end)
			{
				insert(*first);
				first++;
			}
		}

		pair<iterator, bool> insert(const pair< K, V>& data)
		{
			return bt_tree.Insert(data);
		}

		iterator find(const K& K)
		{
			return bt_tree.find(K);
		}
		V& operator[](const K& K)
		{
			
			iterator  it = insert(make_pair(K, V())).first;
			return it->second;
		}


	private:

		RBTree<K, pair<const K, V>, KeyofT> bt_tree;
	};
}